function compose(...middlewares) {
    return function (ctx) {
        function dispatch(i) {
            const fn = middlewares[i];
            if (!fn) return Promise.resolve();
            try {
                return Promise.resolve(fn(ctx, () => dispatch(i + 1)));
            } catch (err) {
                return Promise.reject(err);
            }
        }
        return dispatch(0);
    };
}

async function middleware1(ctx, next) {
    console.log("Middleware 1 before");
    await next();
    console.log("Middleware 1 after");
}

async function middleware2(ctx, next) {
    console.log("Middleware 2 before");
    await next();
    console.log("Middleware 2 after");
}

async function middleware3(ctx, next) {
    console.log("Middleware 3 before");
    await next();
    console.log("Middleware 3 after");
}

const fn = compose(middleware1, middleware2, middleware3);
//? 1b-2b-3b-3a-2a-1a 通过递归实现
// fn({}).catch((err) => console.error(err));
//? 简版
function compose2(...middlewares) {
    function recur(i) {
        const fn = middlewares[i]
        if (!fn) return Promise.resolve()
        try {
            return fn('',() => recur(i + 1))
        }
        catch (err) {
            return Promise.reject(err)
        }
    }
    return recur(0)
}

compose2(middleware1, middleware2, middleware3)
